package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminClass;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IAdminClassService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2020-06-09
 */
@Controller
@RequestMapping("/system/student")
public class AdminStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IAdminClassService adminClassService;
    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student(ModelMap mmap)
    {
        AdminClass adminClass = new AdminClass();
        adminClass.setStatus("0");
        mmap.put("classList", adminClassService.selectAdminClassList(adminClass));
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser sysUser)
    {
        startPage();
        sysUser.setUserType(UserConstants.STUDENT_USER_TYPE);
        List<SysUser> list = userService.selectUserList(sysUser);
        return getDataTable(list);
    }



    /**
     * 新增学生
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
      AdminClass adminClass= new AdminClass();
      adminClass.setStatus("0");
      mmap.put("classList", adminClassService.selectAdminClassList(adminClass));
      return prefix + "/add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUser sysUser)
    {
      if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(sysUser.getLoginName())))
      {
        return error("新增用户'" + sysUser.getLoginName() + "'失败，登录账号已存在");
      }
      else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(sysUser)))
      {
        return error("新增用户'" + sysUser.getLoginName() + "'失败，邮箱账号已存在");
      }
      sysUser.setSalt(ShiroUtils.randomSalt());
      sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), sysUser.getPassword(), sysUser.getSalt()));
      sysUser.setCreateBy(ShiroUtils.getLoginName());
//    if(1!=ShiroUtils.getCompanyId()){
      sysUser.setCompanyId(ShiroUtils.getCompanyId());
      sysUser.setCompanyName(ShiroUtils.getCompanyName());
//    }
      sysUser.setUserType(UserConstants.STUDENT_USER_TYPE);
      return toAjax(userService.insertUser(sysUser));
    }

    /**
     * 修改学生
     */
    @GetMapping("/edit/{userId}")
    public String edit (@PathVariable("userId") Long userId, ModelMap mmap) {
      mmap.put("classList", adminClassService.selectClassByUserId(userId));
      mmap.put("user", userService.selectUserById(userId));
      return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave (SysUser user) {
      userService.checkUserAllowed(user);
      if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
      {
        return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
      }
      else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
      {
        return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
      }
      if(StringUtils.isNotEmpty(user.getPassword())){
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
      }
      user.setUpdateBy(ShiroUtils.getLoginName());
      return toAjax(userService.updateUser(user));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
      try
      {
        return toAjax(userService.deleteUserByIds(ids));
      }
      catch (Exception e)
      {
        return error(e.getMessage());
      }
    }

    /**
     * 学生状态修改
     */
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:student:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus (SysUser sysUser) {
      return toAjax(userService.changeStatus(sysUser));
    }
}
